<?php
class Manuals extends Admin_Controller
{
	
	function __construct()
	{
		parent::__construct();
	}
	
	function index()
	{
		$data['informations'] = new Manual();
		if(@$_GET['search'])$data['informations']->where("title like '%".$_GET['search']."%'");
		if(@$_GET['status'])$data['informations']->where('status',$_GET['status']);
		if(@$_GET['category_id'])$data['informations']->where("category_id = ".$_GET['category_id']);
		$data['informations']->order_by('orderlist','asc')->get_page();
		$this->template->append_metadata(js_lightbox());
		$this->template->append_metadata(js_checkbox('approve'));
		$this->template->build('admin/information_index',$data);
	}
	
	function form($id=FALSE)
	{
		$data['information'] = new Manual($id);
		
		// ไฟล์แนบ
        if($id){
        $data['attachs'] = new Attach();
        $data['attachs']->where("module = 'manuals' and content_id = ".$id)->order_by('id','asc')->get();
        }
		
		$this->template->append_metadata(js_datepicker());
		$this->template->build('admin/information_form',$data);
	}
	
	function save($id=FALSE)
	{
		if($_POST)
		{
			$information = new Manual($id);
			$_POST['title'] = lang_encode($_POST['title']);
			$_POST['detail'] = lang_encode($_POST['detail']);
			$_POST['status'] = 'approve';
			if(!$id)$_POST['user_id'] = $this->session->userdata('id');
			// $_POST['start_date'] = Date2DB($_POST['start_date']);
			// $_POST['end_date'] = Date2DB($_POST['end_date']);
			if($_FILES['image']['name'])
			{
				if($id)$information->delete_file($information->id,'uploads/information/thumbnail','image');
				$information->image = $information->upload($_FILES['image'],'uploads/information/');
			}
			
			//ไฟล์แนบ
            foreach($_POST['file'] as $key=>$item){
                if($item){
                    if(!$id){
                        $id = new Manual($id);
                        $id = $id->order_by('id','desc')->get(1)->id;
                    }
                    $attach = new Attach();
                    $attach->from_array(array(
                        'id' => $_POST['attach_id'][$key],
                        'content_id' => $id,
                        'file_name' => $_POST['file_name'][$key],
                        'file' => $_POST['file'][$key],
                        'module' => 'manuals'
                    ));
                    $attach->save();
                }
            }
			
			$information->from_array($_POST);
			$information->save();
			set_notify('success', lang('save_data_complete'));
		}
		redirect($_POST['referer']);
	}
	
	function approve($id)
	{
		if($_POST)
		{
			$information = new Manual($id);
			$_POST['approve_id'] = $this->session->userdata('id');
			$information->approve_date = date("Y-m-d H:i:s");
			$information->from_array($_POST);
			$information->save();
			echo approve_comment($information);
		}

	}
	
	function delete($id=FALSE)
	{
		if($id)
		{
			$information = new Manual($id);
			$information->delete();
			
			// ลบไฟล์แนบ
			$attachs = new Attach();
            $attachs->where("module = 'manuals' and content_id = ".$id)->get();
            foreach($attachs as $item) $item->delete();
            
			set_notify('success', lang('delete_data_complete'));
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
	
	function save_orderlist($id=FALSE){
        if($_POST)
        {
                foreach($_POST['orderlist'] as $key => $item)
                {
                    if($item)
                    {
                        $manual = new Manual(@$_POST['orderid'][$key]);
                        $manual->from_array(array('orderlist' => $item));
                        $manual->save();
                    }
                }
            set_notify('success', lang('save_data_complete'));
        }
        redirect($_SERVER['HTTP_REFERER']);
    }
	
	function delfile(){
        if($_POST){
            $attach = new Attach($_POST['id']);
            $attach->delete();
        }
    }
}
?>